package cn.bdqn.hospital.dao.impl;

import cn.bdqn.hospital.dao.EmployeesDao;
import cn.bdqn.hospital.entity.Employees;
import cn.bdqn.hospital.utils.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;

public class EmployeesDaoImpl implements EmployeesDao {
    QueryRunner queryRunner = new QueryRunner();

    /**
     * 新增员工
     * @param employees
     * @return
     * @throws SQLException
     */
    @Override
    public int add(Employees employees) throws SQLException {

            String sql = "INSERT INTO employees(ygAccount, ygPhone, ygAddress, ygInductionDate, ygImage, ygName, roleId, ksId,ygpassword) VALUES(?,?,?,?,?,?,?,?,?);";
            Object[] params = new Object[]{employees.getYgAccount(),employees.getYgPhone(),employees.getYgAddress(),employees.getYgInductionDate(),
                    employees.getYgImage(),employees.getYgName(),employees.getRoleId(),employees.getKsId(),employees.getYgPassword()};

        return queryRunner.update(DruidUtils.getConnection(),sql,params);
    }

    /**
     * 修改员工
     * @param employees
     * @return
     * @throws SQLException
     */
    @Override
    public int update(Employees employees) throws SQLException {
        return queryRunner.update(
                "update employees set ygAccount = ?, ygPhone = ?, ygAddress = ?, ygInductionDate = ?, ygImage = ?, ygName = ?, roleId = ?, ksId = ?,ygpassword = ? where ygId = ?;",
                 new Object[]{employees.getYgAccount(),employees.getYgPhone(),employees.getYgAddress(),employees.getYgInductionDate(),
                        employees.getYgImage(),employees.getYgName(),employees.getRoleId(),employees.getKsId(),employees.getYgPassword()});
    }

    /**
     * 通过员工id删除员工
     * @param ygid
     * @return
     * @throws SQLException
     */
    @Override
    public int deleteByYgid(Integer ygid) throws SQLException {
        int num = 0;
        try{
            String sql = "delete from employees where ygId = ?;";
            num = queryRunner.update(DruidUtils.getConnection(),sql,ygid);
        } catch (Exception e){
            e.printStackTrace();
        }

        return num;
    }

    /**
     * 通过员工姓名删除员工
     * @param ygName
     * @return
     * @throws SQLException
     */
    @Override
    public int deleteByYgName(String ygName) throws SQLException {
        int num = 0;
        try{
            String sql = "delete from employees where ygName = ?;";
            num = queryRunner.update(DruidUtils.getConnection(),sql,ygName);
        } catch (Exception e){
            e.printStackTrace();
        }
        return num;
    }

    /**
     * 通过员工账号删除员工
     * @param ygAccount
     * @return
     * @throws SQLException
     */
    @Override
    public int deleteByYgAccount(String ygAccount) throws SQLException {
        int num = 0;
        try{
            String sql = "delete from employees where ygAccount = ?;";
            num = queryRunner.update(DruidUtils.getConnection(),sql,ygAccount);
        } catch (Exception e){
            e.printStackTrace();
        }
        return num;
    }

    /**
     * 通过员工id查询员工
     * @param ygid
     * @return
     * @throws SQLException
     */
    @Override
    public Employees findEmployeesById(Integer ygid) throws SQLException {
        Employees employees = new Employees();
        try{
            String sql = "select ygId,ygAccount, ygPhone, ygAddress, ygInductionDate, ygImage, ygName, roleId, ksId,ygpassword from employees where ygId = ?;";
            employees= (Employees) queryRunner.query(DruidUtils.getConnection(),sql,new BeanListHandler<Employees>(Employees.class),ygid);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return employees;
    }

    /**
     * 通过员工账号查询员工
     * @param ygAccount
     * @return
     * @throws SQLException
     */
    @Override
    public Employees findEmployeesByAccount(String ygAccount) throws SQLException {
        Employees employees = new Employees();
        try{
            String sql = "select ygId,ygAccount, ygPhone, ygAddress, ygInductionDate, ygImage, ygName, roleId, ksId,ygpassword from employees where ygAccount = ?;";
            employees = (Employees) queryRunner.query(DruidUtils.getConnection(),sql,new BeanListHandler<Employees>(Employees.class),ygAccount);
        } catch (Exception e){

        }
        return employees;
    }

    /**
     * 通过员工手机号查询员工
     * @param ygPhone
     * @return
     * @throws SQLException
     */
    @Override
    public Employees findEmployeesByPhone(String ygPhone) throws SQLException {
        Employees employees = new Employees();
        try{
            String sql = "select ygId,ygAccount, ygPhone, ygAddress, ygInductionDate, ygImage, ygName, roleId, ksId,ygpassword from employees where ygPhone = ?;";
            employees = (Employees) queryRunner.query(DruidUtils.getConnection(),sql,new BeanListHandler<Employees>(Employees.class),ygPhone);
        } catch (Exception e){
            e.printStackTrace();
        }
        return employees;
    }

    /**
     * 通过科室id查询员工
     * @param roleId
     * @return
     * @throws SQLException
     */
    @Override
    public Employees findEmployeesByRoleId(String roleId) throws SQLException {
        Employees employees = new Employees();
        try{

        } catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }


}
